import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from scipy import optimize


def func(x, k):
    return k * x


data = pd.read_csv("./data.CSV")
x0 = data["ros"]
y0 = data["car"]

k, eps = optimize.curve_fit(func, x0, y0)
print("拟合的直线为y=%0.4fx，残差为%0.6f" % (k, eps))

x = np.arange(x0[0], x0[len(x0) - 1], 0.01)
y = func(x, k)
plt.figure()
plt.scatter(x0.to_list(), y0.to_list(), 25, "red")
plt.plot(x, y, "purple")
plt.show()
